package com.web.article;

import com.Dao.ArticleDao;
import com.Dao.RemarkDao;
import com.bean.Article;
import com.bean.Remark;
import com.bean.User;
import com.service.ArticleService;
import com.tools.MyTools;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class ArticleServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("action");
		if(action==null)			
			action="";
		if (action.equals("select")) {
			try {
				this.selectArticle(request, response);					//获取某类别下所有文章
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (action.equals("read")) {
			try {
				this.readArticle(request, response);					//阅读文章
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
}

	/**
	 * @功能 从数据表中获取某类别下的所有文章 
	 */
	public void selectArticle(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, SQLException {
		ArticleDao articleDao = new ArticleDao();

		String strType = request.getParameter("typeName");//获取点击的文章分类名
		if (strType == null || strType.equals("")) //没有选择则获取所有文章并显示前五篇文章
			strType = "";
		List articleList=articleDao.queryArticle(strType,"all");
		request.setAttribute("articleList",articleList);
		RequestDispatcher rd=request.getRequestDispatcher("/front/article/ArticleList.jsp");
		rd.forward(request,response);
	}

	public void readArticle(HttpServletRequest request,
							HttpServletResponse response) throws ServletException, IOException, SQLException {
		HttpSession session=request.getSession();
		Article articleBean = new Article();
		ArticleDao articleDao = new ArticleDao();

		int id= Integer.parseInt(request.getParameter("id"));
		String title=request.getParameter("title");
		//System.out.println("title:"+title);
		//int id= MyTools.strToint(strId);
		//int id=Integer.parseInt(strId);

		if (title==null ){
			articleBean.setId(id);
			articleBean=articleDao.queryArticleSingle(id);		//查询指定文章的详细内容
		}else if(title!=null){
			articleBean.setTitle(title);
			articleBean= articleDao.findArticleByTitle(title);

			if(articleBean==null){
				response.sendRedirect("/front/article/Searcherror.jsp");
				return;
			}else{
				id=articleBean.getId();
			}
		}
		articleDao.operationArticle("readTimes", articleBean);//累加阅读次数
		session.setAttribute("readSingle", articleBean);	//保存articleBean到request对象中

		session.setAttribute("articleId",id);

		request.getRequestDispatcher("/DisplayRemark").forward(request, response);


	}

}
